home *** CD-ROM | disk | FTP | other *** search
/ An Introduction to Progr…l Basic 6.0 (4th Edition) / An Introduction to Programming using Visual Basic 6.0.iso / COMMON / TOOLS / VB / CABINETS / MSDAO350.CAB / icontrols / HotButton.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-01-08  |  5.3 KB  |  217 lines

  1. package icontrols;
  2.  
  3. import com.ms.wd.core.Event;
  4. import com.ms.wd.ui.Control;
  5. import com.ms.wd.ui.Graphics;
  6. import com.ms.wd.ui.IButtonControl;
  7. import com.ms.wd.ui.Image;
  8. import com.ms.wd.ui.MouseEvent;
  9. import com.ms.wd.ui.PaintEvent;
  10. import com.ms.wd.ui.Point;
  11. import com.ms.wd.ui.Rectangle;
  12. import com.ms.wd.win32.Windows;
  13.  
  14. public class HotButton extends Control implements IButtonControl {
  15.    private boolean m_pressed = false;
  16.    private boolean m_autoSize = false;
  17.    private boolean m_radio = false;
  18.    private boolean m_focus = false;
  19.    private boolean m_stretch = false;
  20.    private Image m_image = null;
  21.    private Point m_imageSize = null;
  22.    private boolean m_enabled = true;
  23.    private ImageDisplayer m_displayer = new ImageDisplayer();
  24.    private static final int CFOCUS = 3;
  25.    private static final int CBORDER = 4;
  26.  
  27.    protected void onMouseEnter(Event e) {
  28.       if (this.m_enabled) {
  29.          this.m_focus = true;
  30.          if (!this.m_radio && Control.getMouseButtons() != 0) {
  31.             this.m_pressed = true;
  32.          }
  33.  
  34.          ((Control)this).invalidate();
  35.       }
  36.    }
  37.  
  38.    protected void onLostFocus(Event e) {
  39.       if (this.m_enabled) {
  40.          this.m_focus = false;
  41.          ((Control)this).invalidate();
  42.       }
  43.    }
  44.  
  45.    public void setRadio(boolean fRadio) {
  46.       this.m_radio = fRadio;
  47.    }
  48.  
  49.    public boolean getValue() {
  50.       return this.m_pressed;
  51.    }
  52.  
  53.    public boolean isEnabled() {
  54.       return this.m_enabled;
  55.    }
  56.  
  57.    public void setValue(boolean fValue) {
  58.       this.m_pressed = fValue;
  59.       ((Control)this).invalidate();
  60.    }
  61.  
  62.    public boolean isRadio() {
  63.       return this.m_radio;
  64.    }
  65.  
  66.    public HotButton() {
  67.       ((Control)this).setBounds(0, 0, 75, 25);
  68.       ((Control)this).setTabStop(true);
  69.       this.m_displayer.setAlignment(68);
  70.    }
  71.  
  72.    public void onMouseUp(MouseEvent e) {
  73.       if (this.m_enabled) {
  74.          if (!this.m_radio) {
  75.             this.m_pressed = false;
  76.          } else {
  77.             this.m_pressed = !this.m_pressed;
  78.          }
  79.  
  80.          ((Control)this).invalidate();
  81.          this.performClick();
  82.       }
  83.    }
  84.  
  85.    public void setStretch(boolean value) {
  86.       if (value != this.m_stretch) {
  87.          value = this.m_stretch;
  88.          if (value) {
  89.             this.m_displayer.setAlignment(Integer.MIN_VALUE);
  90.             this.m_displayer.setStretch(true);
  91.          } else {
  92.             this.m_displayer.setAlignment(68);
  93.             this.m_displayer.setStretch(false);
  94.          }
  95.  
  96.          ((Control)this).invalidate();
  97.       }
  98.  
  99.    }
  100.  
  101.    private void drawDottedRect(Graphics g, int x, int y, int width, int height, int dashLength) {
  102.       for(int cx = x; cx < x + width; cx += dashLength * 2) {
  103.          int cEnd = Math.min(x + width, cx + dashLength);
  104.          g.drawLine(cx, y, cEnd, y);
  105.          g.drawLine(cx, y + height, cEnd, y + height);
  106.       }
  107.  
  108.       for(int cy = y; cy < y + height; cy += dashLength * 2) {
  109.          int cEnd = Math.min(y + height, cy + dashLength);
  110.          g.drawLine(x, cy, x, cEnd);
  111.          g.drawLine(x + width, cy, x + width, cEnd);
  112.       }
  113.  
  114.    }
  115.  
  116.    public boolean isStretch() {
  117.       return this.m_stretch;
  118.    }
  119.  
  120.    protected void onMouseLeave(Event e) {
  121.       if (this.m_enabled) {
  122.          this.m_focus = false;
  123.          if (!this.m_radio) {
  124.             this.m_pressed = false;
  125.             ((Control)this).invalidate();
  126.          }
  127.  
  128.          ((Control)this).invalidate();
  129.       }
  130.    }
  131.  
  132.    protected void onGotFocus(Event e) {
  133.       if (this.m_enabled) {
  134.          this.m_focus = true;
  135.          ((Control)this).invalidate();
  136.       }
  137.    }
  138.  
  139.    protected void onMouseDown(MouseEvent e) {
  140.       if (this.m_enabled) {
  141.          if (!this.m_radio) {
  142.             this.m_pressed = true;
  143.             ((Control)this).invalidate();
  144.          }
  145.  
  146.          super.onMouseDown(e);
  147.       }
  148.    }
  149.  
  150.    public Image getImage() {
  151.       return this.m_image;
  152.    }
  153.  
  154.    public boolean isAutoSize() {
  155.       return this.m_autoSize;
  156.    }
  157.  
  158.    protected void onResize(Event event) {
  159.       ((Control)this).invalidate();
  160.    }
  161.  
  162.    public void setAutoSize(boolean fAutoSize) {
  163.       this.m_autoSize = fAutoSize;
  164.       if (this.m_image != null) {
  165.          this.setImage(this.m_image);
  166.       }
  167.  
  168.    }
  169.  
  170.    public void performClick() {
  171.       if (this.m_enabled) {
  172.          ((Control)this).onClick(Event.EMPTY);
  173.       }
  174.  
  175.    }
  176.  
  177.    protected void onPaint(PaintEvent pe) {
  178.       Graphics g = pe.graphics;
  179.       Rectangle bounds = ((Control)this).getClientRect();
  180.       Rectangle innerBounds = new Rectangle(bounds.x + 2, bounds.y + 2, bounds.width - 4, bounds.height - 4);
  181.       this.m_displayer.paintIn(g, innerBounds, pe.clipRect, this.m_image, this.m_imageSize);
  182.       if (this.m_enabled) {
  183.          if (this.m_focus && !this.m_pressed) {
  184.             Windows.DrawEdge(g.getHandle(), bounds.toRECT(), 5, 15);
  185.          } else if (this.m_pressed) {
  186.             Windows.DrawEdge(g.getHandle(), bounds.toRECT(), 10, 15);
  187.          }
  188.       }
  189.  
  190.       super.onPaint(pe);
  191.    }
  192.  
  193.    public void setImage(Image image) {
  194.       this.m_image = image;
  195.       this.m_imageSize = image.getSize();
  196.       if (this.m_autoSize && image != null) {
  197.          Point size = image.getSize();
  198.          size.x += 8;
  199.          size.y += 8;
  200.          ((Control)this).setSize(size);
  201.       }
  202.  
  203.       ((Control)this).invalidate();
  204.    }
  205.  
  206.    public void setEnabled(boolean enabled) {
  207.       if (enabled != this.m_enabled) {
  208.          this.m_enabled = enabled;
  209.          ((Control)this).invalidate();
  210.       }
  211.  
  212.    }
  213.  
  214.    public void notifyDefault(boolean fDefault) {
  215.    }
  216. }
  217.